package com.dongdongshop.controller;

import com.dongdongshop.domain.AjaxResult;
import com.dongdongshop.em.ResultEnum;
import com.dongdongshop.pojo.TbOrder;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.OrderService;
import com.dongdongshop.util.Comment;
import com.dongdongshop.vo.CartVo;
import com.dongdongshop.vo.Information;
import com.dongdongshop.vo.OrderVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.xml.ws.Action;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("orderController")
public class OrderController {

    @DubboReference
    private OrderService orderService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private MongoTemplate mongoTemplate;

    // 提交订单数据
    @RequestMapping("addOrder")
    @ResponseBody
    public AjaxResult addOrder(Long addressId,String paymentType){
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
        if (user == null){
            return AjaxResult.R(ResultEnum.TOLOGIN);
        }
        redisTemplate.opsForValue().set("userId", String.valueOf(user.getId()));
        try {
            OrderVO orderVO = orderService.addOrder(addressId,paymentType,user);
            return AjaxResult.OK().setData(orderVO);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //查看订单信息
    @RequestMapping("getOrderSend")
    @ResponseBody
    public AjaxResult getOrderSend(){
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
        if (user == null){
            return AjaxResult.R(ResultEnum.TOLOGIN);
        }
        try {
            List<Information> cartVos = orderService.getOrderSend(user.getUsername());
            return AjaxResult.OK().setData(cartVos);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //查看待评价订单
    @RequestMapping("getEvalute")
    @ResponseBody
    public AjaxResult getEvalute(){
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
        if (user == null){
            return AjaxResult.R(ResultEnum.TOLOGIN);
        }
        try {
            List<Information> cartVos = orderService.getEvalute(user.getUsername());
            return AjaxResult.OK().setData(cartVos);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }


    @RequestMapping("addEvalute")
    @ResponseBody
    public AjaxResult addEvalute(Comment comment){
        if (comment == null){
            return AjaxResult.R(ResultEnum.PARAMETER_EMPTY);
        }
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
        comment.setUserId(String.valueOf(user.getId()));
        comment.setDate(new Date());


        try {
            mongoTemplate.insert(comment);
            return AjaxResult.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //点赞增加
    @RequestMapping("likeInc")
    @ResponseBody
    public AjaxResult likeInc(String id){
        Query query = new Query(Criteria.where("id").is(id));
        Update update = new Update();
        update.inc("like");
        mongoTemplate.updateFirst(query, update,Comment.class);
        return AjaxResult.OK();
    }

    //    评论查询
//    分页并以星级查询
    @RequestMapping("limitMongo")
    @ResponseBody
    public AjaxResult findThis(String xing,Long pageNum , Integer pageSize){
        if(xing != null && xing != ""){
            Query query = new Query(Criteria.where("dengji").is(xing));
            query.skip(pageNum).limit(pageSize);
//            query.addCriteria(Criteria.where("dengji").is(xing)).skip(pageNum).limit(pageSize);
            List<Comment> comments = mongoTemplate.find(query, Comment.class);
            return AjaxResult.OK().setData(comments);
        }
        Query query = new Query();
        query.skip(pageNum).limit(pageSize);
        List<Comment> comments = mongoTemplate.find(query, Comment.class);
        return AjaxResult.OK().setData(comments);
    }
}
